package com.zjxfood.http;

import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.BinaryHttpResponseHandler;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.zjxfood.common.Constants;

public class AsyUtils {

	private static AsyncHttpClient client = new AsyncHttpClient(); // 实例话对象
	static {
		client.setTimeout(11000); // 设置链接超时，如果不设置，默认为10s
	}

	public static void get(String urlString, AsyncHttpResponseHandler res) // 用一个完整url获取一个string对象
	{
		client.get(urlString, res);
	}

	public static void get(String urlString, RequestParams params,
			AsyncHttpResponseHandler res) // url里面带参数
	{
		String time = System.currentTimeMillis() + "";
		String sign = "";
		if (params.toString().equals("")) {
			sign = Constants.sortsStr("timestamp=" + time);
		} else {
			sign = Constants.sortsStr(params.toString() + "&timestamp=" + time);
		}
		params.put("partner", "app_android_new");
		params.put("sign", sign);
		params.put("timestamp", time);
		Log.i("params", "url:"+urlString + params);
		client.get(urlString, params, res);
	}
	public static void post(String urlString, RequestParams params,
			AsyncHttpResponseHandler res){
		String time = System.currentTimeMillis() + "";
		String sign = "";
		if (params.toString().equals("")) {
			sign = Constants.sortsStr("timestamp=" + time);
		} else {
			sign = Constants.sortsStr(params.toString() + "&timestamp=" + time);
		}
		params.put("partner", "app_android_new");
		params.put("sign", sign);
		params.put("timestamp", time);
		Log.i("params", "url:"+urlString + params);
		client.post(urlString, params, res);
	}

	public static void get(String urlString, JsonHttpResponseHandler res) // 不带参数，获取json对象或者数组
	{
		client.get(urlString, res);
	}

	public static void get(String urlString, RequestParams params,
			JsonHttpResponseHandler res) // 带参数，获取json对象或者数组
	{
		String time = System.currentTimeMillis() + "";
		String sign = "";
		if (params.toString().equals("")) {
			sign = Constants.sortsStr(params.toString() + "timestamp=" + time);
		} else {
			sign = Constants.sortsStr(params.toString() + "&timestamp=" + time);
		}
		params.put("partner", "app_android_new");
		params.put("sign", sign);
		params.put("timestamp", time);
		Log.i("params", "url:"+urlString + params);
		client.get(urlString, params, res);
		//slither.io
	}

	public static void get(String uString, BinaryHttpResponseHandler bHandler) // 下载数据使用，会返回byte数据
	{
		client.get(uString, bHandler);
	}

	public static AsyncHttpClient getClient() {
		return client;
	}

}
